package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateActivitiesGetPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateActivitiesSaveDTO;
import com.itlong.cloud.POJO.PO.OperateActivitiesInfoPO;
import com.itlong.cloud.POJO.VO.operate.OperateActivitiesGetPageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateActivitiesDao;
import com.itlong.cloud.operate.service.IOperateActivitiesService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-APP-启动页管理业务处理接口实现类
 * </desc>
 *
 * @createDate 2019/06/24
 */
@Service
public class OperateActivitiesServiceImpl implements IOperateActivitiesService {
    @Autowired
    IOperateActivitiesDao iOperateActivitiesDao;

    /**
     * <desc>
     *      获取分页数据
     * </desc>
     *
     * @param dto 入参
     * @return page模型
     * @author Qiang.S
     * @createDate 2019/06/24
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateActivitiesGetPageVO> getActivitiesPage(OperateActivitiesGetPageDTO dto) throws Exception {
        //分页类默认pageSize为20
        Page<OperateActivitiesGetPageVO> page = new Page<>();
        page.getPage().setPageSize("15");
        page.getPage().setCurrentPage("1");
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto, page);
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperateActivitiesDao.getPage(params), iOperateActivitiesDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      添加启动页
     * </desc>
     *
     * @param dto 功能实体
     * @return sql执行行数
     * @author Qiang.S
     * @createDate 2019/06/24
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer save(OperateActivitiesSaveDTO dto) throws Exception {
        Date nowTime = new Date();
        OperateActivitiesInfoPO po = new OperateActivitiesInfoPO();
        BeanUtils.copyProperties(dto,po);
        po.setActivitiesId(LogicIdUtil.bussinessId());
        po.setStatus(DataStatusEnum.NORMAL.getType());
        po.setCreateTime(nowTime);
        po.setUpdateTime(nowTime);
        po.setStartTime( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dto.getStartTime()));
        po.setEndTime( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dto.getEndTime()));
        return iOperateActivitiesDao.save(SqlUtil.durableData(po, PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      更新功能
     * </desc>
     *
     * @param dto 功能实体
     * @return sql执行行数
     * @author Qiang.S
     * @createDate 2019/06/24
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer update(OperateActivitiesSaveDTO dto) throws Exception {
        Date nowTime = new Date();
        OperateActivitiesInfoPO po = new OperateActivitiesInfoPO();
        BeanUtils.copyProperties(dto,po);
        po.setUpdateTime(nowTime);
        po.setStartTime( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dto.getStartTime()));
        po.setEndTime( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dto.getEndTime()));
        return iOperateActivitiesDao.update(SqlUtil.durableData(po, PlatformConstants.TABLE_UPDATE));
    }

    /**
     * <desc>
     *      通过id获取功能
     * </desc>
     *
     * @param activitiesId 活动编号
     * @return 资源实体
     * @author Qiang.S
     * @createDate 2019/06/24
     */
    @Override
    public OperateActivitiesGetPageVO getOne(String activitiesId) {
        return iOperateActivitiesDao.getOne(activitiesId);
    }

    /**
     * <desc>
     *      删除功能
     * </desc>
     *
     * @param activitiesIds 活动编号,逗号分隔
     * @return sql执行行数
     * @author Qiang.S
     * @createDate 2019/06/24
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer delete(String activitiesIds) {
        String activitiesId[] = StringHandlerUtil.splitString(activitiesIds);
        return iOperateActivitiesDao.delete(activitiesId);
    }

    /**
     * <desc>
     *      获取运营活动可用数据
     * </desc>
     *
     * @return page模型
     * @author Qiang.S
     * @createDate 2019/06/24
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public List<OperateActivitiesGetPageVO> OperationalAdvertising(String projectId) {
        return iOperateActivitiesDao.OperationalAdvertising(projectId);
    }
}
